//
//  XMAcceptMeetView.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/8.
//

#import "XMAcceptMeetView.h"

@interface XMAcceptMeetView ()

@property (nonatomic, strong) UIImageView *contentImgView;
@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIButton *getBtn;

//@property (nonatomic, copy) NSString *payPrice;
//@property (nonatomic, copy) NSString *serviceId;

@end

@implementation XMAcceptMeetView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEXA(0x464646, 0.46);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
    [self addGestureRecognizer:tap];
    
    [self addSubview:self.contentImgView];
    [_contentImgView addSubview:self.avatarImgView];
    [_contentImgView addSubview:self.titleLabel];
    UIImageView *meetBgImgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width - KScaleWidth(190.0))/2, _titleLabel.maxY + KScaleWidth(16.0), KScaleWidth(190.0), KScaleWidth(213.0))];
    meetBgImgView.image = ImageNamed(@"chat_get_meet_detail_bg_img");
    meetBgImgView.userInteractionEnabled = YES;
    [_contentImgView addSubview:meetBgImgView];
    {
        [meetBgImgView addSubview:self.priceLabel];
        _priceLabel.y = meetBgImgView.height - KScaleWidth(21.0) - _priceLabel.height;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, KScaleWidth(36.0), meetBgImgView.width, KScaleWidth(42.0)) font:KBoldFontSize(28.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentCenter];
        titleLabel.text = @"见面包";
        [meetBgImgView addSubview:titleLabel];
        
        UIButton *chatDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(13.0) frame:CGRectMake(0, titleLabel.maxY + KScaleWidth(5.0), meetBgImgView.width, KScaleWidth(36.0)) title:@"查看见面包详情" titleColor:kColorHEX(0x3B5998)];
        [chatDetailBtn addTarget:self action:@selector(chatMeetDetailAction:) forControlEvents:UIControlEventTouchUpInside];
        [meetBgImgView addSubview:chatDetailBtn];
        [meetBgImgView addSubview:self.priceLabel];
        _priceLabel.y = meetBgImgView.height - KScaleWidth(21.0) - _priceLabel.height;
    }
    [_contentImgView addSubview:self.getBtn];
    _getBtn.y = meetBgImgView.maxY + KScaleWidth(20.0);
    self.alpha = 0;
}

- (void)setContentWithMeetDic:(NSDictionary *)dic {
    NSString *type = [dic safeStringForKey:@"orderType"];
    _priceLabel.hidden = YES;
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:[dic safeStringForKey:@"avatar"]]];
    if ([type isEqualToString:@"1"]) {
        _priceLabel.hidden = NO;
        _priceLabel.text = [NSString stringWithFormat:@"转卖价格：¥%@", [dic safeStringForKey:@"payPrice"]];
        [_getBtn setTitle:@"购买" forState:UIControlStateNormal];
        _titleLabel.text = [NSString stringWithFormat:@"%@\n向你转卖了一个见面包", [dic safeStringForKey:@"nickName"]];
    }
    else {
        [_getBtn setTitle:@"领取" forState:UIControlStateNormal];
        _titleLabel.text = [NSString stringWithFormat:@"%@\n向你转赠了一个见面包", [dic safeStringForKey:@"nickName"]];
    }
}

- (void)disMiss {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (void)getAction:(UIButton *)button {
    self.meetGetBlock ? self.meetGetBlock() : nil;
}

- (void)chatMeetDetailAction:(UIButton *)button {
    self.chatMeetDetailBlock ? self.chatMeetDetailBlock() : nil;
}

- (void)show {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.alpha = 1;
    }];
}

#pragma mark - Init
- (UIImageView *)contentImgView {
    if (!_contentImgView) {
        _contentImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, KScaleWidth(87.0), self.width, KScaleWidth(579.0))];
        _contentImgView.image = ImageNamed(@"chat_get_meet_bg_img");
        _contentImgView.userInteractionEnabled = YES;
    }
    return _contentImgView;
}

- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width - KScaleWidth(56.0))/2, KScaleWidth(113.0), KScaleWidth(56.0), KScaleWidth(56.0))];
    }
    return _avatarImgView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.avatarImgView.maxY + KScaleWidth(16.0), self.width, KScaleWidth(50.0))];
        _titleLabel.numberOfLines = 0;
        _titleLabel.font = [UIFont fontWithName:@"" size:KScaleWidth(24.0)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(190.0), KScaleWidth(32.0)) font:KFontSize(16.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
    }
    return _priceLabel;
}

- (UIButton *)getBtn {
    if (!_getBtn) {
        _getBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, 0, KScaleWidth(320.0), KScaleWidth(44.0)) title:@"购买" titleColor:kColorHEX(0x333333)];
        [_getBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
        [_getBtn setCornerRadius:_getBtn.height/2];
        [_getBtn addTarget:self action:@selector(getAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _getBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
